package classtrain;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @DESCRIPATION
 * @AUTHOR 戴超
 * @DATE 2018/6/19
 **/
public class UpperStringSocketServerTest {
    public static void main(String[] args) throws Exception {
        ServerSocket server = null; //  声明ServerSocket 对象
        Socket client = null; //  一个Socket 对象表示一个客户端
        server = new ServerSocket(8888);// 服务器在8888 端口上等待客户端访问
        System.out.println(" 服务器运行, 等待客户端连接");
        client = server.accept(); //  程序再此阻塞, 等待客户端连接
        System.out.println("连接成功");

        InputStream input = client.getInputStream();
        OutputStream output = client.getOutputStream();
        int available = 0;
        StringBuilder sb = new StringBuilder();
        while ((available = input.available()) > 0) {
            byte[] bytes = new byte[1024];
            input.read(bytes, 0, available);
            sb.append(new String(bytes));
        }
        String string = sb.toString();
        System.out.println("接收到的字符串" + string);
        byte[] re = string.toUpperCase().getBytes();
        output.write(re, 0, re.length);
        output.flush();
        output.close();

        input.close();
        client.close(); //  关闭客户端连接
        server.close(); //
    }


}